//
//  AppointmentStore.m
//  WorkManager
//
//  Created by iD Student on 7/17/12.
//  Copyright (c) 2012 iD Student. All rights reserved.
//

#import "AppointmentStore.h"

@implementation AppointmentStore
@synthesize allAppointmentReports;
- (id)init
{
    self = [super init];
    if (self) {
        //Allocate the memory for the array and initialize it for future use
        self.allAppointmentReports = [[NSMutableArray alloc] init];
    }
    return self;
}

#pragma mark ----------Appointment Manipulation Methods----------

//This method sorts the appointments by their date in the array.
//The appointments with earlier dates will be sent down in the view
- (void)sortAppointments
{
    if([self.allAppointmentReports count] > 1)
    {
        int changes=1;
        while (changes!=0)
        {
            changes = 0;
            for (int i=0; i<([self.allAppointmentReports count]-1); i++) {
                NSDate *iDate = [[self.allAppointmentReports objectAtIndex:i] date];
                NSDate *iPlusDate = [[self.allAppointmentReports objectAtIndex:(i+1)] date];
                if ([[iDate earlierDate:iPlusDate] isEqualToDate:iDate]) {
                    AppointmentReport *tempAppointmentReport = [[self.allAppointmentReports objectAtIndex:i] retain];
                    [self.allAppointmentReports removeObjectAtIndex:i];
                    [self.allAppointmentReports insertObject:tempAppointmentReport atIndex:(i+1)];
                    [tempAppointmentReport release];
                    tempAppointmentReport = nil;
                    changes++;
                }
            }
        }
    }
}

#pragma mark ----------Table View Data Methods----------

//Return a new appointment report that has been added to the array
//The returned appointment report will also be set as the current appointment.
//The list view should then go straight to an editing page for the report
- (AppointmentReport *)createAppointmentReport
{
    //First alloc and init a new appointment report
    AppointmentReport *tempAppointmentReport = [[AppointmentReport alloc] init];
    
    //Now add the new appointment report to the array
    [self.allAppointmentReports addObject:tempAppointmentReport];
        
    //Now do memory management on the appointment report because all the pointers are set how they should be
    //We don't need this reference any more
    [tempAppointmentReport release];
    tempAppointmentReport = nil;

    //Sort the appointments
    [self sortAppointments];
    
    //Now return the current appointment report
    return [self.allAppointmentReports objectAtIndex:([allAppointmentReports count]-1)];
}

//Remove an appointment report identical to the one passed
- (void)removeAppointmentReportAtIndex:(int)index
{
    //Remove the object from the array that's at the index passed
    [self.allAppointmentReports removeObjectAtIndex:index];
}

#pragma mark ----------Saving and Loading Methods----------

//Encode the class for saving
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:self.allAppointmentReports forKey:@"allAppointmentReports"];
}

//Decode the class from a saved file
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        self.allAppointmentReports = [decoder decodeObjectForKey:@"allAppointmentReports"];
    }
    return self;
}

@end
